package com.sooth.system.dept.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.sooth.common.core.domain.R;
import com.sooth.common.log.annotation.Log;
import com.sooth.common.web.core.BaseController;
import com.sooth.system.dept.service.ISysDeptService;
import com.sooth.system.dept.view.SysDeptBo;
import com.sooth.system.dept.view.SysDeptDto;
import com.sooth.system.dept.view.SysDeptVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门信息
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dept")
public class SysDeptController extends BaseController {

    private final ISysDeptService deptService;

    /**
     * 获取部门列表
     */
    @SaCheckPermission("system:dept:query")
    @GetMapping("/list")
    public R<List<SysDeptVo>> list(SysDeptDto deptDto) {
        return R.ok(deptService.selectDeptTree(deptDto));
    }


    /**
     * 根据部门编号获取详细信息
     *
     * @param id 部门ID
     */
    @SaCheckPermission("system:dept:query")
    @GetMapping(value = "/query")
    public R<SysDeptBo> query(@RequestParam Long id) {
        return R.ok(deptService.selectById(id));
    }

    /**
     * 新增部门
     */
    @SaCheckPermission("system:dept:add")
    @Log(title = "部门管理", message = "新增部门")
    @PostMapping("/add")
    public R<Void> save(@Validated @RequestBody SysDeptBo dept) {
        return toAjax(deptService.insert(dept));
    }

    /**
     * 修改部门
     */
    @SaCheckPermission("system:dept:edit")
    @Log(title = "部门管理", message = "修改部门")
    @PutMapping("/edit")
    public R<Void> edit(@Validated @RequestBody SysDeptBo dept) {
        return toAjax(deptService.updateById(dept));
    }

    /**
     * 删除部门
     *
     * @param id 部门ID
     */
    @SaCheckPermission("system:dept:remove")
    @Log(title = "部门管理", message = "删除部门")
    @DeleteMapping("/remove")
    public R<Void> remove(@RequestParam Long id) {
        return toAjax(deptService.deleteById(id));
    }
}
